package com.cxhome.cxinfox.provider;

import com.cxhome.cxinfox.cxdepartment.DepartmentService;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.traffic.managent.annotation.ControllerBind;
import com.traffic.managent.base.CxBaseController;
import com.traffic.managent.model.product.Provider;
import com.traffic.managent.util.SQLUtils;
import com.traffic.managent.util.StringUtils;
import com.traffic.managent.util.UtilsUtils;

import sun.security.jca.Providers;

/**
 * 供应商产品管理
 * 
 * @author Guoxing
 *
 */
@ControllerBind(controllerKey = "/system/providerinfo")
public class ProviderController extends CxBaseController {

	private static final String path = "/pages/system/providerinfo/providerinfo_";

	public void index() {
		list();
	}

	public void list() {
		Provider model = getModelByAttr(Provider.class);
		StringBuilder orderSql = new StringBuilder("");
		// 排序
		String orderBy = getBaseForm().getOrderBy();
		if (StringUtils.isEmpty(orderBy)) {
			orderSql.append(" order by  pi.createTime desc , pi.providerName desc");
		} else {
			orderSql.append(" order by ").append(orderBy.trim());
		}
		Page<Provider> page = ProviderService.service.paginate(model, getPaginator(), orderSql);

		setAttr("selectProvider", ProviderService.service.selectProvider(model.getStr("ids")));
		setAttr("page", page);
		setAttr("attr", model);
		render(path + "list.html");
	}
//	public void list() {
//		Provider model = getModelByAttr(Provider.class);
//		
//		SQLUtils sql = new SQLUtils(" from cx_provider pi where 1 = 1  ");
//		
//		if (model.getAttrValues().length != 0) {
//			sql.setAlias("pi");
//			sql.whereLike("ids", model.getStr("ids"));
//			sql.whereEquals("delFlag", model.getStr("delFlag"));
//			sql.whereEquals("channel", model.getStr("channel"));
//		}
//		
//		
//		// 排序
//		String orderBy = getBaseForm().getOrderBy();
//		if (StringUtils.isEmpty(orderBy)) {
//			sql.append(" order by  pi.createTime desc , pi.providerName desc");
//		} else {
//			sql.append(" order by ").append(orderBy);
//		}
//		
//		Page<Provider> page = Provider.dao.paginate(getPaginator(), "select pi.*  ", sql.toString().toString());
//		setAttr("selectProvider", ProviderService.service.selectProvider(model.getStr("ids")));
//		setAttr("page", page);
//		setAttr("attr", model);
//		render(path + "list.html");
//	}

	public void add() {
		setAttr("departSelect", DepartmentService.service.selectDepart(""));
		render(path + "add.html");
	}

	public void view() {
		Provider model = Provider.dao.findById(getPara());

		setAttr("model", model);
		render(path + "view.html");
	}

	/**
	 * 删除功能：逻辑删除
	 */
	public void delete() {
		String ids = getPara();
		String username = getSessionUser().getUserName();
		String delFlag = getPara("delFlag");
		ProviderService.service.delete(ids, username, delFlag);
		list();
	}

	/**
	 * 跳转编辑页面
	 */
	public void edit() {
		Provider model = Provider.dao.findById(getPara());
		setAttr("model", model);
		render(path + "edit.html");
	}

	/**
	 * 保存功能
	 */
	public void save() {
		String pid = getPara();
		Provider model = getModel(Provider.class);
		String userid = getSessionUser().getUserName();
		ProviderService.service.saveOrUpdate(pid, model, userid);

		renderMessage("保存成功");
	}

}
